package models

import (
	"coding.net/vco/mder/modules/coding"
	//"coding.net/vco/mder/modules/log"
	"coding.net/vco/mder/modules/util"
	"fmt"
	"github.com/BurntSushi/toml"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
)

//基本字段
//url  //TODO 自定义url,增加一个映射
//title  标题
//keywords  数组 关键字
//description 描述
//author 作者
//pubDate 发布日期
//content 内容
//views 点击数

func Article_Detail(path string) map[string]interface{} {
	filepath := fmt.Sprintf("%s/%s%s", coding.DATA_PATH, path, util.MD_EXT)
	tomlConfig, _ := ioutil.ReadFile(util.TdMdToml(filepath)) //
	v := map[string]interface{}{}
	toml.Decode(string(tomlConfig), v)
	content, _ := ioutil.ReadFile(util.ToMdContent(filepath))
	v["content"] = string(content)
	v["url"] = path
	return v
}

// 文章列表，不携带内容
func Article_List(path string, limit, skip int) []map[string]interface{} {
	list := make([]map[string]interface{}, 0)
	filepath.Walk(fmt.Sprintf("%s/%s", coding.DATA_PATH, path), func(path string, info os.FileInfo, err error) error {
		if info == nil || strings.HasPrefix(info.Name(), ".") { //忽略.开头文件
			//go
			return nil
		}

		if util.IsMd(info.Name()) {
			tomlConfig, _ := ioutil.ReadFile(util.TdMdToml(path))
			v := map[string]interface{}{}
			toml.Decode(string(tomlConfig), v)
			//content, _ := ioutil.ReadFile(util.TdMdToml(path))
			//v["content"] = string(content)
			rel, _ := filepath.Rel(coding.DATA_PATH, path)
			if util.IsMd(rel) {
				rel = util.Substr(rel, 0, len(rel)-3)
			}
			v["url"] = rel

			list = append(list, v)

		}
		return nil
	})

	return list
}
